package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"path/filepath"
	"pet-pal/cache"
	"pet-pal/models"
	"time"
)

type CuteController struct {
	beego.Controller
}

func (c *CuteController) Add() {

	img := c.GetString("img")
	name := c.GetString("name")
	revise := c.GetString("revise")
	create := c.GetString("create")
	oid, _ := c.GetInt("oid")
	cute := models.Cute{
		Img:    img,
		Name:   name,
		Revise: revise,
		Create: create,
		Oid:    oid,
	}
	err := models.DB.Create(&cute).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "添加失败",
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "添加成功",
		}
		c.ServeJSON()
	}
}
func (c *CuteController) Upload() {
	file, head, err := c.GetFile("img")
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "文件获取失败",
		}
		c.ServeJSON()
		return
	}
	if head.Size > 2*1024*1024 {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "文件过大",
		}
		c.ServeJSON()
		return
	}
	ext := filepath.Ext(head.Filename)
	if ext != ".jpg" && ext != ".png" {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "文件格式不正确",
		}
		c.ServeJSON()
		return
	}
	defer file.Close()
	path := "static/img/" + head.Filename
	err = c.SaveToFile("img", path)
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "文件上传失败",
		}
		c.ServeJSON()
		return
	} else {
		Url := "http://127.0.0.1:8080/" + path
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "文件上传成功",
			Data: Url,
		}
		c.ServeJSON()
		return
	}
}
func (c *CuteController) List() {

	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 3)
	offset := (page - 1) * size
	query := models.DB.Model(&models.Cute{})
	var count int64
	query.Count(&count)
	var cute []models.Cute
	cuteList := "cute_list"
	jsonData, _ := cache.Rdb.Get(cache.Ctx, cuteList).Result()
	if jsonData == "" {
		err := query.Offset(offset).Limit(size).Find(&cute).Error
		if err != nil {
			c.Data["json"] = Res{
				Code: 0,
				Msg:  "查询失败",
			}
			c.ServeJSON()
			return

		}
		cacheData, _ := json.Marshal(cute)
		cache.Rdb.Set(cache.Ctx, cuteList, cacheData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cuteList), &cute)
	}
	PageData := make(map[string]interface{})
	PageData["count"] = count
	PageData["cute"] = cute
	c.Data["json"] = Res{
		Code: 200,
		Msg:  "查询成功",
		Data: PageData,
	}
	c.ServeJSON()

}
func (c *CuteController) Del() {

	id, _ := c.GetInt("id")
	err := models.DB.Delete(&models.Cute{}, id).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "删除失败",
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "删除成功",
		}
		c.ServeJSON()
	}
}
func (c *CuteController) Show() {

	id, _ := c.GetInt("id")
	var cute models.Cute
	cuteShow := "cute_shoe"
	jsonData, _ := cache.Rdb.Get(cache.Ctx, cuteShow).Result()
	if jsonData == "" {
		err := models.DB.Where("id=?", id).First(&cute).Error
		if err != nil {
			c.Data["json"] = Res{
				Code: 0,
				Msg:  "查询失败",
			}
			c.ServeJSON()
			return
		}
		cacheData, _ := json.Marshal(cute)
		cache.Rdb.Set(cache.Ctx, cuteShow, cacheData, time.Hour*2)

	} else {
		json.Unmarshal([]byte(cuteShow), &cute)
	}
	c.Data["json"] = Res{
		Code: 200,
		Msg:  "查询成功",
		Data: cute,
	}
	c.ServeJSON()

}
